package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        List<SetmealVO> list = setmealMapper.select(setmealPageQueryDTO);

        Page<SetmealVO> page = (Page<SetmealVO>) list;
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //插入套餐的信息
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();

        //插入菜品和套餐的关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && !setmealDishes.isEmpty()) {
            //设置setmealId字段 和 dishId
            setmealDishes.forEach(e -> {
                e.setSetmealId(setmealId);
                //查询菜品对应的名称
                Long dishId = dishMapper.selectDishNameByDishId(e.getName());
                e.setDishId(dishId);
            });
            setmealDishMapper.insert(setmealDishes);
        }
    }

    @Override
    public void setStatus(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.update(setmeal);
    }

    @Override
    public SetmealVO selectById(Long id) {
        //查询套餐有关的信息
        Setmeal setmeal = setmealMapper.selectById(id);
        //查询套餐下的菜品信息
        List<SetmealDish> list = setmealDishMapper.selectDishNameBySetmealName(id);
        //根据category_id查询相对于的分类名字
        String categoryName = categoryMapper.selectCategoryName(setmeal.getCategoryId());

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setCategoryName(categoryName);
        setmealVO.setSetmealDishes(list);

        return setmealVO;
    }

    @Transactional
    @Override
    public void update(SetmealVO setmealVO) {
        //修改套餐的相关信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO, setmeal);
        setmealMapper.update(setmeal);
        //先删除该套餐下的菜品,再增加改套餐下的菜品
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        if (setmealDishes != null && !setmealDishes.isEmpty()) {
            setmealDishes.forEach(e -> {
                e.setSetmealId(setmealVO.getId());
            });
            setmealDishMapper.deleteBySetmealId(setmealVO.getId());
            setmealDishMapper.insert(setmealDishes);
        }

    }

    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        //删除套餐相关信息
        setmealMapper.deleteByIds(ids);
        //删除套餐关联的菜品
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }


}
